#include <iostream>

using namespace std;

int main()
{
    int T,y,n,count;
    cin>>T;
    while(T--)
    {
        cin>>y>>n;
        count=0;
        for(count=0;count<n;y++)
        {
            if((y%4==0 && y%100!=0)||y%400==0)   count++;
        }
        cout<<y-1<<endl;
    }
    return 0;
}
